package com.zyz.I_多线程.单例模式.饿汉式;

/**
 * @author: vick
 * @Date: 2025/3/22
 * @Description:
 * @version: 1.0
 */
public class Test {

    static Singleton singleton1 = null;
    static Singleton singleton2 = null;
    public static void main(String[] args) {
        /*
        为了不让new，产生不一样的对象，所以构造器私有化
        Singleton singleton1 = new Singleton();
        Singleton singleton2 = new Singleton();

        System.out.println(singleton1 == singleton2);//false
        */

        /*
        Singleton instance1 = Singleton.getINSTANCE();
        Singleton instance2 = Singleton.getINSTANCE();
        System.out.println(instance1 == instance2);//true
        */

        Thread thread1 = new Thread() {
            @Override
            public void run() {
                singleton1 = Singleton.getINSTANCE();
            }
        };

        Thread thread2 = new Thread() {
            @Override
            public void run() {
                singleton2 = Singleton.getINSTANCE();
            }
        };

        thread1.start();
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(singleton2 == singleton1); //true
    }
}
